package com.neusoft.dynamicagent;

import com.neusoft.staticagent.UmsAdminServiceImpl;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: duan
 * @Date: Create in 11:00 2020/10/31
 */
public class CglibDemo {
    public static void main(String[] args) {
        final UmsAdminServiceImpl target = new UmsAdminServiceImpl();
        //创建增强器
        Enhancer enhancer = new Enhancer();
        // 设置父类
        enhancer.setSuperclass(UmsAdminServiceImpl.class);
        //设置回调
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] args,
                                    MethodProxy methodProxy) throws Throwable {
                System.out.println("前置代码增强....");
                Object invoke = method.invoke(target, args);
                System.out.println("后置代码增强....");
                return invoke;
            }
        });
        UmsAdminServiceImpl proxy = (UmsAdminServiceImpl) enhancer.create();
        proxy.insert(null);
    }
}
